<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\IntegralGoods;
use think\Db;
/**
 *  积分商品管理
 *
 *  Author:  YSX
    Date:    2022-11-30
 */
class Integral extends Api
{
    protected $noNeedLogin = ['goodsList','goodsDetail'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
    /*
     * 列表
    */
    public function goodsList()
    {
        // $params = $this->params;
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		$where['is_del'] = array('eq',1);
		$where['is_enable'] = array('eq',1);
		if(isset($params['state']) && !empty($params['state'])){
			$where['state'] = array('eq',$params['state']);
		}
		if(isset($params['order']) && !empty($params['order'])){
			if($params['order'] == 'asc'){
				$order = 'price asc';
			}else{
				$order = 'price desc';
			}
		}else{
			$order = 'sort asc';
		}
		$limit    = !empty($params['limit']) ? $params['limit'] : 10;
		$page     = !empty($params['page']) ? $params['page'] : 1;
		// $order
        $data['list'] = IntegralGoods::where($where)
			  ->order($order)
			  ->field('id,goods_name,image,price,state,add_time,lineation_price,mix_title,sort')
			  ->page($page)
			  ->limit($limit)
			  ->select(); 
		foreach($data['list'] as $key => $value){
			$data['list'][$key]['attr'] = Db::name('integral_goods_attr')->where('integral_goods_id',$value['id'])->column('attr_name');
		}
		$data['count']    = IntegralGoods::where($where)->count();
		if ($data) {
			$this->success('获取成功', $data);
		}
		$this->error('请求失败');
    }

    /*
     * 详情
     */
    public function goodsDetail()
    {
        if (!$this->request->isPost()) {
        	return $this->error('提交方式不正确');
        }
		$params = $this->request->post();
		if(!isset($params['goods_id']) || empty($params['goods_id'])){
			return $this->error('参数错误');
		}
		$where['is_del'] = array('eq',1);
		$where['is_enable'] = array('eq',1);
		$data = IntegralGoods::where('id', $params['goods_id'])
			->where($where)
			->field('id,goods_name,image,carousel_image,state,price,guarantee,introduction,add_time,lineation_price,mix_title')
			->find();
		$data['carousel_image'] = \explode(',',$data['carousel_image']);
		$data['attr'] = Db::name('integral_goods_attr')->where('integral_goods_id',$params['goods_id'])->field('id,attr_name')->select();
		$this->success('获取成功', $data);
    }
	/*
	 * 获取规格图片
	 */
	public function getAttrImage(){
		if (!$this->request->isPost()) {
			return $this->error('提交方式不正确');
		}
		$params = $this->request->post();
		$data = Db::name('integral_goods_attr')->where('id',$params['id'])->field('id,attr_name,image,price')->find();
		if($data){
			$this->success('获取成功', $data);
		}else{
			$this->error('获取失败');
		}
	}
	/*
	 * 加入购物车
	 */
	public function addCart()
	{
	    if (!$this->request->isPost()) {
	    	return $this->error('提交方式不正确');
	    }
		$params = $this->request->post();
		$member_id = $this->auth->id;
		$where['member_id'] = array('eq',$member_id);
		$where['integral_goods_id'] = array('eq',$params['integral_goods_id']);
		$where['integral_goods_attr_id'] = array('eq',$params['integral_goods_attr_id']);
		//判断这个商品属性是否存在
		$isFind = Db::name('integral_cart')->where($where)->find();
		if($isFind){
			//则数量添加
			$result = Db::name('integral_cart')->where($where)->setInc('number',$params['number']);
		}else{
			//则添加
			$data['member_id'] = $member_id;
			$data['integral_goods_id'] = $params['integral_goods_id'];
			$data['integral_goods_attr_id'] = $params['integral_goods_attr_id'];
			$data['number'] = $params['number'];
			$data['state'] = $params['state'];
			$result = Db::name('integral_cart')->insert($data);
		}
		if ($result) {
			$this->success('加入购物车成功');
		}
		$this->error('加入购物车失败');
	}
}
